package com.bocai.dahuatech.ui.adapter;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bocai.dahuatech.Bean.ShopCardListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.activity.NewProductDetailActivity;
import com.bocai.dahuatech.utils.DialogUtil;
import com.bocai.dahuatech.utils.KeyBoardUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

public class MyShoppingcardAdapter extends BaseAdapter
{
    private Context context;
    public Map<Integer, Boolean> selectedMap;
    public HashSet<String> delContactsIdSet;
    List<ShopCardListBean.ContentEntity> datas;
    public MyShoppingcardAdapter(Context context ,List<ShopCardListBean.ContentEntity> datas)
    {
        this.context=context;
        this.datas=datas;
        // 保存每条记录是否被选中的状态
        selectedMap = new HashMap<>();
        // 保存被选中记录作数据库表中的Id
        delContactsIdSet = new HashSet<>();

        for (int i = 0; i < datas.size(); i++)
        {
            if(datas.get(i).getFStatus()==0)
            {
                selectedMap.put(i, false);
            }else if (datas.get(i).getFStatus()==1)
            {
                if(!datas.get(i).isFFitting())
                {
                    selectedMap.put(i, true);
                }else
                {
                    selectedMap.put(i, false);
                }
            }
        }
    }
    @Override
    public int getCount()
    {
        return datas.size();
    }

    @Override
    public Object getItem(int position)
    {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        if(convertView==null)
        {
            viewHolder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.shoppingcard_item,null);
            viewHolder.cb_shoppingcard_product= (CheckBox) convertView.findViewById(R.id.cb_shoppingcard_product);
            viewHolder.ivPic= (ImageView) convertView.findViewById(R.id.iv_shopcardlist_pic);
            viewHolder.tvModel= (TextView) convertView.findViewById(R.id.tv_shopcardlist_model);
            viewHolder.tvName= (TextView) convertView.findViewById(R.id.tv_shopcardlist_name);
            viewHolder.tvPrice= (TextView) convertView.findViewById(R.id.tv_shopcardlist_price);
            viewHolder.tvTotalCount= (TextView) convertView.findViewById(R.id.tv_shopcardlist_totalcount);
            viewHolder.tvTotalPrice= (TextView) convertView.findViewById(R.id.tv_shopcardlist_totalprice);
            viewHolder.tvDown= (TextView) convertView.findViewById(R.id.tv_shopcardlist_down);
            viewHolder.tvAdd= (TextView) convertView.findViewById(R.id.tv_shopcardlist_add);
            viewHolder.llView=(LinearLayout)convertView.findViewById(R.id.ll_shoppingcart_view);
            convertView.setTag(viewHolder);
        }else
        {

            viewHolder= (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(datas.get(position).getFPicPath())
//                .error(R.mipmap.product_photo_defalt)
                .into(viewHolder.ivPic);
        if(datas.get(position).isFFitting())
        {
            viewHolder.cb_shoppingcard_product.setVisibility(View.INVISIBLE);

        }else
        {
            viewHolder.cb_shoppingcard_product.setVisibility(View.VISIBLE);
        }
        viewHolder.tvModel.setText(datas.get(position).getFOutModel());
        viewHolder.tvName.setText(datas.get(position).getFItemName());
        if(SharePrefencesUtil.getFAccessFlag(context)==2)
        {
            viewHolder.tvPrice.setText(context.getResources().getString(R.string.money)+datas.get(position).getFStandPrice());
            if(datas.get(position).isFFitting())
            {
                viewHolder.tvTotalPrice.setText(R.string.subtotal0);
            }else
            {
                viewHolder.tvTotalPrice.setText(context.getResources().getString(R.string.subtotal) + (datas.get(position).getFStandPrice() * datas.get(position).getFBQty()));
            }
        }else
        {
            viewHolder.tvPrice.setText("");
            viewHolder.tvTotalPrice.setText("");
        }
        viewHolder.tvTotalCount.setText(datas.get(position).getFBQty() + "");
        viewHolder.cb_shoppingcard_product.setChecked(selectedMap.get(position));
        viewHolder.cb_shoppingcard_product.setOnClickListener(new MyOnClickListener(position, viewHolder.tvTotalCount, viewHolder.tvTotalPrice));
        viewHolder.tvDown.setOnClickListener(new MyOnClickListener(position,viewHolder.tvTotalCount,viewHolder.tvTotalPrice));
        viewHolder.tvAdd.setOnClickListener(new MyOnClickListener(position,viewHolder.tvTotalCount,viewHolder.tvTotalPrice));
        viewHolder.tvTotalCount.setOnClickListener(new MyOnClickListener(position,viewHolder.tvTotalCount,viewHolder.tvTotalPrice));
        viewHolder.llView.setOnClickListener(new MyOnClickListener(position,viewHolder.tvTotalCount,viewHolder.tvTotalPrice));
        viewHolder.llView.setOnLongClickListener(new MyLongClickLitener(position, viewHolder.tvTotalCount, viewHolder.tvTotalPrice));
        return convertView;
    }
    class MyLongClickLitener implements View.OnLongClickListener
    {
        private int position;
        private TextView tvTotalCount,tvTotalPrice;
        int totalCount=1;
        public MyLongClickLitener(int position,View tvTotalCount,View tvTotalPrice)
        {
            this.position=position;
            this.tvTotalCount= (TextView) tvTotalCount;
            this.tvTotalPrice= (TextView) tvTotalPrice;

        }
        @Override
        public boolean onLongClick(View v)
        {
            DialogUtil.dialogForTwoButton(context, R.string.deleteproduct, R.string.dismiss,R.string.confirm , new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    totalCount=Integer.parseInt((tvTotalCount).getText().toString());
                    EventBus.getDefault().post(new MainEvent(context.getResources().getString(R.string.settlement),position,datas.get(position).getFEntryID(),totalCount,0,5));
                    DialogUtil.dismissTowDialog();
                }
            });
            return false;
        }
    }
    class MyOnClickListener implements View.OnClickListener
    {
        private int position;
        private TextView tvTotalCount;
        int totalCount=1;
        int totalCountnew;
        public MyOnClickListener(int position,View tvTotalCount,View tvTotalPrice)
        {
            this.position=position;
            this.tvTotalCount= (TextView) tvTotalCount;

        }
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.ll_shoppingcart_view:
                    // TODO: 2016/3/29  
                    if(!datas.get(position).isFFitting()){
                        Intent intent = new Intent(context, NewProductDetailActivity.class);
                        intent.putExtra("productId", datas.get(position).getFItemID());
                        intent.putExtra("solutionId", "0");
                        context.startActivity(intent);
                    }
                    break;
                case R.id.tv_shopcardlist_down:
                    totalCount=Integer.parseInt((tvTotalCount).getText().toString());
                    if(totalCount>1)
                    {
                        EventBus.getDefault().post(new MainEvent(context.getResources().getString(R.string.settlement),position,datas.get(position).getFEntryID(),totalCount,0,2));
                    }
                    break;
                case R.id.tv_shopcardlist_add:
                    totalCount=Integer.parseInt(tvTotalCount.getText().toString());
                    totalCount++;
                    EventBus.getDefault().post(new MainEvent(context.getResources().getString(R.string.settlement),position,datas.get(position).getFEntryID(),totalCount,0,3));
                    break;
                case R.id.tv_shopcardlist_totalcount:
                    totalCount=Integer.parseInt(tvTotalCount.getText().toString());
                    totalCountnew=Integer.parseInt(tvTotalCount.getText().toString());
                    View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_edit_productnum, null);
                    final Dialog dialog = new AlertDialog.Builder(context)
                            .setView(dialogView).show();
                    TextView tvadd = (TextView) dialog.findViewById(R.id.tv_editshop_add);
                    TextView tvdown = (TextView) dialog.findViewById(R.id.tv_editshop_down);
                    TextView tvdismiss = (TextView) dialog.findViewById(R.id.tv_editshop_dismiss);
                    final TextView tvtrue = (TextView) dialog.findViewById(R.id.tv_editshop_true);
                    final EditText etcount = (EditText) dialog.findViewById(R.id.et_editshop_count);
                    //软键盘弹出
                    KeyBoardUtils.openKeybord(etcount,context);
                    etcount.setText(totalCount + "");
                    etcount.setSelection(etcount.getText().length());
                    etcount.addTextChangedListener(new TextWatcher()
                    {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after)
                        {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count)
                        {

                        }

                        @Override
                        public void afterTextChanged(Editable s)
                        {
                            if(!s.toString().equals(""))
                            {
                                totalCountnew=Integer.parseInt(s.toString());
                            }
                        }
                    });
                    tvadd.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v) {
                            totalCountnew++;
                            etcount.setText(totalCountnew + "");
                            etcount.setSelection(etcount.getText().length());
                        }
                    });
                    tvdown.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            if (totalCountnew > 1)
                            {
                                totalCountnew--;
                                etcount.setText(totalCountnew + "");
                                etcount.setSelection(etcount.getText().length());
                            }
                        }
                    });
                    tvdismiss.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v) {
                            KeyBoardUtils.closeKeybord(etcount, context);
                            dialog.dismiss();
                        }
                    });
                    tvtrue.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v) {
                            if("".equals(etcount.getText().toString())){
                                Toast.makeText(context,R.string.notless,Toast.LENGTH_SHORT).show();
                                KeyBoardUtils.closeKeybord(etcount, context);
                                dialog.dismiss();
                            }else if(0==Integer.parseInt(etcount.getText().toString()))
                            {
                                Toast.makeText(context,R.string.notmore,Toast.LENGTH_SHORT).show();
                                KeyBoardUtils.closeKeybord(etcount, context);
                                dialog.dismiss();
                            }else if(100000<Integer.parseInt(etcount.getText().toString()))
                            {
                                Toast.makeText(context,R.string.notmore,Toast.LENGTH_SHORT).show();
                                KeyBoardUtils.closeKeybord(etcount, context);
                                dialog.dismiss();
                            }else if(!"".equals(etcount.getText().toString()))
                            {

                                totalCountnew=Integer.parseInt(etcount.getText().toString());
                                EventBus.getDefault().post(new MainEvent(context.getResources().getString(R.string.settlement), position, datas.get(position).getFEntryID(), totalCountnew, totalCountnew - totalCount, 4));
                                KeyBoardUtils.closeKeybord(etcount, context);
                                dialog.dismiss();
                            }

                        }
                    });

                    break;
                case R.id.cb_shoppingcard_product:
                    totalCount=Integer.parseInt((tvTotalCount).getText().toString());
                    boolean cb = !selectedMap.get(position);
                    selectedMap.put(position, cb);
                    if(cb)
                    {
                        //选中
                        EventBus.getDefault().post(new MainEvent(context.getResources().getString(R.string.settlement),position,datas.get(position).getFEntryID(),totalCount,0,1));
                    }else
                    {
                        //取消选中
                        EventBus.getDefault().post(new MainEvent(context.getResources().getString(R.string.settlement),position,datas.get(position).getFEntryID(),totalCount,0,0));
                    }
                    break;
            }
        }
    }
    public class ViewHolder
    {
        LinearLayout llView;
        CheckBox cb_shoppingcard_product;
        TextView tvModel,tvName,tvPrice,tvTotalCount,tvTotalPrice,tvDown,tvAdd;
        ImageView ivPic;
    }
}
